package com.zt.rongda.shelter.service;

import java.util.List;

import com.zt.rongda.shelter.inter.ShelterMapper;
import com.zt.rongda.shelter.model.Shelter;
import com.zt.system.model.BusinessException;
import com.zt.servlet.ReqBean;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ShelterService extends BaseService implements IShelterService {

    @Autowired
    private ShelterMapper shelterMapper;

    /**
    * 查询
    *
    * @param reqBean
    * @return
    */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("select * from MEDIA_SHELTER a where 1=1 ");
        sqlBuilder.addWhere("a.SHELTER_NO", reqBean.getInfo("shelterNo"));
        sqlBuilder.addWhere("a.ORIGINAL_NO", reqBean.getInfo("originalNo"));
        sqlBuilder.addWhere("a.AREA_NO", reqBean.getInfo("areaNo"));
        sqlBuilder.addWhere("a.STOP_NAME",reqBean.getInfo("stopName"));
        sqlBuilder.addWhere("a.C_COUNTY",reqBean.getInfo("county"));
        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
    * 通过id查询
    *
    * @param shelterId
    * @return
    */
    public Shelter getById(String shelterId) {
        return shelterMapper.selectByPrimaryKey(shelterId);
    }

    /**
    * 新增
    *
    * @param shelter
    */
    public void add(Shelter shelter) {
        shelter.setShelterId(intIncrementId("MEDIA_SHELTER", "SHELTER_ID"));// 手动设置Id

        shelter.setMakeTime(new Date());
        shelter.setModifyTime(shelter.getMakeTime());
        shelterMapper.insert(shelter);
    }

    /**
    * 修改
    *
    * @param shelter
    */
    public void update(Shelter shelter) {
        String shelterId = shelter.getShelterId();
        if (StringUtil.isEmpty(shelterId)) {
            throw new BusinessException("获取主键失败");
        }

        shelter.setModifyTime(new Date());
        shelterMapper.updateByPrimaryKeySelective(shelter);
    }

    /**
    * 删除
    *
    * @param shelterId
    */
    public void delete(String shelterId) {
        if (StringUtil.isEmpty(shelterId)) {
            throw new BusinessException("获取主键失败");
        }
        shelterMapper.deleteByPrimaryKey(shelterId);
    }

}